package com.ruoyi.project.hcy.product.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.product.domain.style.ProStyle;
import com.ruoyi.project.hcy.product.service.IProStyleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProStyleController
 * @Author SAI
 * @Date 2020/9/18 8:48
 * @Description ProStyleController
 * @Version 1.0
 */
@Controller
@RequestMapping("hcy/product/proStyle")
public class ProStyleController extends BaseController {

    @Autowired
    private IProStyleService proStyleService;



    private String prefix = "hcy/product/style";

    /**
     * 产品风格的页面跳转
     * @return
     */
    @RequiresPermissions("product:proStyle:view")
    @GetMapping()
    public String operlog(){
        return prefix+"/proStyle";
    }

    /**
     * 产品风格查询集合，包含分页、条件查询
     * @param proStyle
     * @return
     */
    @RequiresPermissions("product:proStyle:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProStyle proStyle){
        startPage();
        List<ProStyle> list = proStyleService.selectProStyleList(proStyle);
        return getDataTable(list);
    }

    /**
     * 添加页面转发
     * @return
     */
    @GetMapping("/add")
    public String add(){
        return prefix +"/add";
    }

    /**
     * 新增保存产品风格
     * @param proStyle
     * @return
     */
    @RequiresPermissions("product:proStyle:add")
    @Log(title = "产品风格管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProStyle proStyle){
        if ("1".equals(proStyleService.checkStyleNameUnique(proStyle))) {
            return error("新增风格'" + proStyle.getStyleName() + "'失败，风格名称已存在");
        }
        return toAjax(proStyleService.insertProStyle(proStyle));
    }


    /**
     * 检测产品风格名字是否唯一
     * @param proStyle
     * @return
     */
    @PostMapping("/checkStyleNameUnique")
    @ResponseBody
    public String checkStyleNameUnique(ProStyle proStyle) {
        return proStyleService.checkStyleNameUnique(proStyle);
    }

    /**
     * 根据id查询style做数据回显，并转发edit页面
     * @param styleId 风格id
     * @param mmap  model集合
     * @return  页面
     */
    @GetMapping("/edit/{styleId}")
    public String edit(@PathVariable("styleId") Long styleId, ModelMap mmap){
        mmap.put("style",proStyleService.selectProStyleById(styleId));
        return prefix+"/edit";
    }

    /**
     * 风格修改保存
     * @param proStyle
     * @return
     */
    @RequiresPermissions("product:proStyle:edit")
    @Log(title = "产品风格管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ProStyle proStyle){
        if ("1".equals(proStyleService.checkStyleNameUnique(proStyle))) {
            return error("修改风格'" + proStyle.getStyleName() + "'失败，风格名称已存在");
        }
        return toAjax(proStyleService.updateProStyle(proStyle));
    }


    /**
     * 风格删除
     * @param ids
     * @return
     */
    @RequiresPermissions("product:proStyle:remove")
    @Log(title = "产品风格管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(proStyleService.deleteProStyleByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

}
